package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import basic.Club;
import basic.League;

import kontroler.Kontroler;

@SuppressWarnings("serial")
public class CenterPanel extends JScrollPane{

	private JTable table = null;
	private DefaultTableModel tableModel = null;
	private JPopupMenu tablePopUp = null;
	private JMenuItem addClub = null;
	private JMenuItem editClub = null;
	private JMenuItem removeClub = null;
	private JMenuItem close = null;
	private Kontroler kontroler = null;
	private WestPanel westPanel = null;


	public CenterPanel(WestPanel westPanel) {
		super();
		initialize();
		this.westPanel = westPanel;
		this.kontroler = westPanel.getKontroler();
	}

	private void initialize(){
		this.setViewportView(getTable());
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				if (e.getButton() == 3){
					super.mouseReleased(e);
					getTablePopUp().setVisible(true);
					getTablePopUp().show(null, e.getXOnScreen(), e.getYOnScreen());
				}
			}
		});
	}
	private DefaultTableModel getTableModel() {
		if (tableModel == null) {
			tableModel = new DefaultTableModel();
			tableModel.addColumn("#");
			tableModel.addColumn("Club");
			tableModel.addColumn("Win");
			tableModel.addColumn("Draw");
			tableModel.addColumn("Lost");
			tableModel.addColumn("Goals");
			tableModel.addColumn("Concieved");
			tableModel.addColumn("Difference");
			tableModel.addColumn("Points");
		}
		return tableModel;
	}
	public JTable getTable() {
		if (table == null) {
			table = new JTable(getTableModel());
			table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
			table.setFont(new Font("Algerian", Font.PLAIN, 14));
			JTableHeader header = table.getTableHeader();
			header.setBackground(Color.lightGray);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					super.mouseReleased(e);
					if (e.getButton() == 3){
						tablePopUp.setVisible(true);
						tablePopUp.setLocation(e.getPoint());
					}
				}
			});
		}
		return table;
	}

	private JPopupMenu getTablePopUp(){
		if (tablePopUp == null){
			tablePopUp = new JPopupMenu();
			tablePopUp.add(getAddClub());
			tablePopUp.add(getEditClub());
			tablePopUp.add(getRemoveClub());
			tablePopUp.addSeparator();
			tablePopUp.add(getClose());
		}
		return tablePopUp;
	}

	private JMenuItem getAddClub() {
		if (addClub == null){
			addClub = new JMenuItem("Add Club");
			addClub.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					tablePopUp.setVisible(false);
					String country = westPanel.getCompSplitPane().getSelectedCountry().getName();
					League league = westPanel.getCompSplitPane().getSelectedLeague();
					String name = JOptionPane.showInputDialog(null,"Country: "+country+"\nLeague: "+league.getName()+"\nEnter club name: ",JOptionPane.OK_CANCEL_OPTION);
					kontroler.addClub(name, league);
					populateTable();
				}
			});
		}
		return addClub;
	}
	private JMenuItem getEditClub() {
		if (editClub == null){
			editClub = new JMenuItem("Edit Club");
		}
		return editClub;
	}
	private JMenuItem getRemoveClub() {
		if (removeClub == null){
			removeClub = new JMenuItem("Remove Club");
		}
		return removeClub;
	}
	private JMenuItem getClose() {
		if (close == null){
			close = new JMenuItem("Close");
			close.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					tablePopUp.setVisible(false);				
				}
			});
		}
		return close;
	}
	private void populateTable(){
		for (int j = tableModel.getRowCount()-1;j >= 0;j--){
			tableModel.removeRow(j);
		}
		LinkedList<Club> clubs = westPanel.getCompSplitPane().getSelectedLeague().getClubs();
		for(int i = 0; i < clubs.size(); i++){
			tableModel.addRow(clubs.get(i).getClubLeagueView());
		}
	}
	public Kontroler getKontroler(){
		return kontroler;
	}
	public String getSelectedClubValue(){
		try {
		int row = table.getSelectedRow();
		int col = 1;
		return (String)table.getValueAt(row, col);
		} catch (NullPointerException e) {
			return "";
		}
	}
	public Club getSelectedClub(){
		try {
			int row = table.getSelectedRow();
			int col = 1;
			return kontroler.getClub((String)table.getValueAt(row, col), westPanel.getCompSplitPane().getSelectedLeague());
		} catch (NullPointerException e) {
		JOptionPane.showMessageDialog(this, "Select a league");
		return null;
		}
	}
	public WestPanel getWestPanel(){
		return westPanel;
	}

}
